package com.etc.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.etc.controller.LoginController;
import com.etc.controller.SerializeOperation;
import com.etc.dao.CareerClassifyDAO;
import com.etc.dao.EquipmentDAO;
import com.etc.entity.Career;
import com.etc.entity.CareerClassify;
import com.etc.entity.Counter;
import com.etc.entity.Equipment;
import com.etc.entity.Role;
import com.etc.entity.RoleEquipment;
import com.etc.service.CareerService;
import com.etc.service.EquipmentService;
import com.etc.service.RoleEquipmentService;
import com.etc.service.RoleService;

public class SelectEquip {
	
	LoginController loginController = new LoginController();
	EquipmentService equipmentSer = new EquipmentService();
	RoleEquipmentService reSer = new RoleEquipmentService();
	RoleService roleSer = new RoleService();
	CareerService careerSer = new CareerService();


	public void selecEquip(JPanel panel, String classId, JFrame jFrame, Counter counter,DataOutputStream dos, DataInputStream dis) {

		// 获取各种装备，方便循环输出
		ArrayList<Equipment> weapons = equipmentSer.findEquipByType("主手武器");
		ArrayList<Equipment> helmets = equipmentSer.findEquipByType("头部");
		ArrayList<Equipment> tops = equipmentSer.findEquipByType("上装");
		ArrayList<Equipment> bottoms = equipmentSer.findEquipByType("下装");
		ArrayList<Equipment> shoes = equipmentSer.findEquipByType("鞋子");

		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		// panel.setPreferredSize(new Dimension(x,y));

		JTextPane name = new JTextPane();
		name.setBounds(1500, 100, 100, 250);
		name.setFont(new Font("楷体", Font.BOLD, 16));
		// name.setEnabled(false);
		name.setText("昵称输入");
		panel.add(name);
		
		//选择武器
		JLabel pWeapons = new JLabel("主武器：");
		pWeapons.setBounds(10, 90, 60, 35);
		panel.add(pWeapons);
		// 循环输出到下拉框
		JComboBox<Object> j1 = new JComboBox<>();
		for (int i = 0; i < weapons.size(); i++) {
			j1.addItem(weapons.get(i).getEquipName());
		}
		panel.add(j1);

		//选择头部
		JLabel head = new JLabel("头部：");
		head.setBounds(10, 10, 40, 35);
		panel.add(head);
		// 循环输出到下拉框
		JComboBox<Object> j2 = new JComboBox<>();
		for (int i = 0; i < helmets.size(); i++) {
			j2.addItem(helmets.get(i).getEquipName());
		}
		panel.add(j2);

		//选择上衣
		JLabel clothes = new JLabel("上衣：");
		clothes.setBounds(10, 50, 40, 35);
		panel.add(clothes);
		// 循环输出到下拉框
		JComboBox<Object> j3 = new JComboBox<>();
		for (int i = 0; i < tops.size(); i++) {
			j3.addItem(tops.get(i).getEquipName());
		}
		panel.add(j3);

		//选择下装
		JLabel Pants = new JLabel("下装：");
		Pants.setBounds(10, 170, 40, 35);
		panel.add(Pants);
		// 循环输出到下拉框
		JComboBox<Object> j4 = new JComboBox<>();
		for (int i = 0; i < bottoms.size(); i++) {
			j4.addItem(bottoms.get(i).getEquipName());
		}
		panel.add(j4);

		//选择鞋子
		JLabel shoe = new JLabel("鞋子：");
		shoe.setBounds(10, 200, 40, 35);
		panel.add(shoe);
		// 循环输出到下拉框
		JComboBox<Object> j5 = new JComboBox<>();
		for (int i = 0; i < shoes.size(); i++) {
			j5.addItem(shoes.get(i).getEquipName());
		}
		panel.add(j5);

		JButton jButton = new JButton("创建角色");
		jButton.setBounds(100, 700, 1500, 150);
		panel.add(jButton);

		JOptionPane jop = new JOptionPane();

		// 监听提交按钮
		jButton.addActionListener(e -> {
			int n = JOptionPane.showConfirmDialog(null, "是否确认添加角", "添加角色？", JOptionPane.YES_NO_OPTION);
			if (n == 0) {
				Equipment weapon = weapons.get(j1.getSelectedIndex());
				Equipment helmet = helmets.get(j2.getSelectedIndex());
				Equipment top = tops.get(j3.getSelectedIndex());
				Equipment bottom = bottoms.get(j4.getSelectedIndex());
				Equipment shoe1 = shoes.get(j5.getSelectedIndex());
				Equipment second = equipmentSer.findEquipById("038efbe0");
				RoleEquipment re = new RoleEquipment(weapon, second, helmet, top, bottom, shoe1);
				reSer.addRoleEquip(re);
				Career career = careerSer.findCareerByClassId(classId);
				Role addRole = roleSer.addRole(name.getText(), career, re, counter.getId());
				counter.getRoles().add(addRole);
				// 回到主目录
				SerializeOperation.saveToFile(counter, counter.getId()+".dat");
				Counter counternew = SerializeOperation.loadFromFile(counter.getId()+".dat");
				GameLoading gl=new GameLoading(counternew, dos, dis, panel);
				gl.setTitle(counter.getLoginId());
				gl.pack();
				gl.setVisible(true);
				gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gl.setLocationRelativeTo(null);
				jFrame.setVisible(false);
			}
		});
	}

	
	public static void main(String[] args) {

		JFrame frame = new JFrame("装备界面");
		frame.setBounds(100, 100, 400, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		// 添加面板
		frame.add(panel);

		// selecEquip(panel);

		frame.setVisible(true);

	}
}
